package com.csyf.manager.controller;

import com.csyf.manager.entity.SysAliAccount;
import com.csyf.manager.service.SysAliAccountBiz;
import com.csyf.manager.utils.ResultEntity;
import com.csyf.manager.utils.ResultUtli;
import com.csyf.manager.utils.page.JqGridVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Map;

/**
 * @Author: casper
 * @Description: 支付宝Controller
 * @Date: Created in 2018/10/25 18:37
 */
@Slf4j
@Controller
@RequestMapping(value = "/boss/ali")
public class SysAliController {

    @Autowired
    private SysAliAccountBiz sysAliAccountBiz;

    @GetMapping(value = "view/{pageName}")
    public String viewPage(@PathVariable(value = "pageName") String pagename) {
        return "boss/ali/" + pagename;
    }

    /**
     * 支付宝账户列表
     *
     * @param vo
     * @param rows
     * @param page
     * @return
     */
    @ResponseBody
    @PostMapping(value = "aliAccountDataList")
    public JqGridVo aliAccountDataList(SysAliAccount vo, Integer rows, Integer page) {
        return sysAliAccountBiz.querydata(vo, page, rows);
    }    /**



     * 支付宝账户余额列表*
     * @param vo
     * @param rows
     * @param page
     * @return
     */
    @ResponseBody
    @PostMapping(value = "accounBalancetDataList")
    public JqGridVo accounBalancetDataList(SysAliAccount vo, Integer rows, Integer page) {
        return sysAliAccountBiz.queryAccountBalance(vo, page, rows);
    }

    /**
     * 保存支付宝账户
     */
    @ResponseBody
    @PostMapping(value = "saveAliAccount")
    public ResultEntity saveAliAccount(SysAliAccount vo) {
        try {
            //添加
            if (null == vo.getId()) {
                sysAliAccountBiz.saveAccount(vo);
                return ResultUtli.DEFAULT_SUCCESS("账户添加成功！", null);
            } else {
                int i = sysAliAccountBiz.updateAccount(vo);
                if (i > 0) {
                    return ResultUtli.DEFAULT_SUCCESS("账户修改成功！", null);
                } else {
                    return ResultUtli.ERRO(10, "账户修改失败！", null);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtli.ERRO(10, e.getMessage(), null);
        }
    }

    /**
     * 根据id查询支付宝账户
     *
     * @param accountId
     * @return
     */
    @ResponseBody
    @PostMapping(value = "findAliAccountById")
    public ResultEntity findAliAccountById(Long accountId) {
        try {
            SysAliAccount aliAccount = sysAliAccountBiz.findById(accountId);
            return ResultUtli.DEFAULT_SUCCESS("成功", aliAccount);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtli.ERRO(10, e.getMessage(), null);
        }
    }

    /**
     * 删除支付宝账户
     *
     * @param accountId
     * @return
     */
    @ResponseBody
    @PostMapping(value = "deleteAliAccountById")
    public ResultEntity deleteAliAccountById(Long accountId) {
        //判断账户是否启用，启用不能删除
        SysAliAccount aliAccount = sysAliAccountBiz.findById(accountId);
        if (aliAccount.getChannelStatus() == 1) {
            return ResultUtli.ERRO(10, "通道开启中，不能删除！", null);
        }
        sysAliAccountBiz.deleteAccount(accountId);
        return ResultUtli.DEFAULT_SUCCESS("账户删除成功！", null);
    }


    /**
     * 停用账户
     *
     * @param accountId
     * @return
     */
    @ResponseBody
    @PostMapping(value = "stopaccount")
    public ResultEntity stopaccount(Long accountId) {
        //判断账户是否启用，启用不能删除
        sysAliAccountBiz.stopaccount(accountId);
        return ResultUtli.DEFAULT_SUCCESS("账户停用成功！", null);
    }

    /**
     * 启用账户
     *
     * @param accountId
     * @return
     */
    @ResponseBody
    @PostMapping(value = "startaccount")
    public ResultEntity startaccount(Long accountId) {
        //判断账户是否启用，启用不能删除
        sysAliAccountBiz.startaccount(accountId);
        return ResultUtli.DEFAULT_SUCCESS("账户启用成功！", null);
    }


    /**
     * 分配商户 ，对应的商户列表
     *
     * @param accountId
     * @return
     */
    @ResponseBody
    @PostMapping(value = "merhcantlist")
    public ResultEntity merhcantlist(Long accountId) {
        ArrayList<Map> merhcantlist = sysAliAccountBiz.merhcantlist(accountId);
        return ResultUtli.DEFAULT_SUCCESS("查询成功！", merhcantlist);
    }


    /**
     * 分配商户 ，设置费率
     *
     * @param accountId
     * @return
     */
    @ResponseBody
    @PostMapping(value = "issueMerchant")
    public ResultEntity issueMerchant(Long accountId, Long agentId, String rate) {
        sysAliAccountBiz.issueMerchant(accountId, agentId, rate);
        return ResultUtli.DEFAULT_SUCCESS("设置成功！", null);
    }
}
